<template>
  <div class="index-lists">
    <el-card class="!border-none" shadow="never">
      <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
        <el-form-item label="所属模块" prop="module">
          <el-select v-model="queryParams.module" class="w-[280px]" clearable>
            <el-option label="全部" value=""/>
            <el-option label="线索" value="0"/>
            <el-option label="客户" value="1"/>
            <el-option label="商机" value="2"/>
            <el-option label="合同" value="3"/>
          </el-select>
        </el-form-item>
        <el-form-item label="跟进方式" prop="type">
          <el-select v-model="queryParams.type" class="w-[280px]" clearable>
            <el-option label="全部" value=""/>
            <el-option label="电话" value="0"/>
            <el-option label="QQ" value="1"/>
            <el-option label="拜访" value="2"/>
            <el-option label="微信" value="3"/>
            <el-option label="邮件" value="4"/>
            <el-option label="短信" value="5"/>
            <el-option label="其他" value="6"/>
          </el-select>
        </el-form-item>
        <el-form-item label="跟进时间" prop="followUpTime">
          <daterange-picker
              v-model:startTime="queryParams.createTimeStart"
              v-model:endTime="queryParams.createTimeEnd"
          />
        </el-form-item>

        <el-form-item label="创建人" prop="userId">
          <el-select v-model="queryParams.userId" class="w-[280px]" clearable>
            <el-option label="张三" value="1"/>
            <el-option label="李四" value="2"/>
            <el-option label="王五" value="3"/>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="resetPage">查询</el-button>
          <el-button @click="resetParams">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="!border-none mt-4" shadow="never">
      <div>
        <!--<el-button v-perms="['cluepool:add']" type="primary" @click="handleAdd">-->
        <!--  <template #icon>-->
        <!--    <icon name="el-icon-Plus"/>-->
        <!--  </template>-->
        <!--  新增-->
        <!--</el-button>-->
        <el-button v-perms="['cluepool:export']" type="primary">
          <template #icon>
            <icon name="el-icon-download" />
          </template>
          导出
        </el-button>
      </div>
      <el-table class="mt-4" size="large" v-loading="pager.loading" :data="pager.lists" stripe>
        <el-table-column type="selection" width="55"/>
        <el-table-column label="ID" prop="id" min-width="50"/>
        <el-table-column label="跟进记录名称" sortable prop="name" min-width="180"/>
        <el-table-column label="跟进时间" sortable prop="followUpTime" min-width="180"/>
        <el-table-column label="所属模块" sortable prop="module" min-width="130"/>
        <el-table-column label="跟进方式" sortable prop="type" min-width="130"/>
        <el-table-column label="跟进状态" sortable prop="status" min-width="130"/>
        <el-table-column label="跟进人员" sortable prop="followUpName" min-width="130"/>
        <el-table-column label="联系人" sortable prop="linkmanName" min-width="130"/>
        <el-table-column label="联系方式" sortable prop="phone" min-width="130"/>
        <el-table-column sortable label="跟进内容" prop="followUpContent" min-width="130"/>
        <el-table-column
            sortable
            label="下次跟进时间"
            prop="nextFollowUpTime"
            min-width="180"
        />
        <el-table-column sortable label="创建时间" prop="createTime" min-width="180"/>
        <!-- <el-table-column label="操作" width="120" fixed="right">
  <template #default="{ row }" slot-scope="props">
    <el-button
      v-perms="['followup:comment']"
      type="primary"
      link
      @click="handleEdit(row)"
    >
      评论
    </el-button>
  </template>
</el-table-column> -->
      </el-table>
      <div class="flex justify-end mt-4">
        <pagination v-model="pager" @change="getLists"/>
      </div>
    </el-card>
    <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false"/>
  </div>
</template>
<script lang="ts" setup name="followup">
import {followupDelete, followupLists} from '@/api/followup'
import {usePaging} from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'

const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
const queryParams = reactive({
  followUpTimeStart: '',
  followUpTimeEnd: '',
  module: '',
  type: '',
  userId: ''
})

const {pager, getLists, resetPage, resetParams} = usePaging({
  fetchFun: followupLists,
  params: queryParams
})

const handleAdd = async () => {
  showEdit.value = true
  await nextTick()
  editRef.value?.open('add')
}

const handleEdit = async (data: any) => {
  showEdit.value = true
  await nextTick()
  editRef.value?.open('edit')
  editRef.value?.getDetail(data)
}

const handleDelete = async (id: number) => {
  await feedback.confirm('确定要删除？')
  await followupDelete({id})
  feedback.msgSuccess('删除成功')
  getLists()
}

getLists()
</script>
